package conversion.controller;

import com.alibaba.fastjson2.JSONObject;
import conversion.plugin.ConvertPlugin;
import conversion.plugin.PluginManager;
import conversion.utils.converter.AllConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class ConvertController {

    @Resource
    AllConverter converter;

    @Resource
    PluginManager pluginManager;

    @PostMapping("/convert/{convertType}")
    public String convert(@RequestBody String textData,
                          @PathVariable("convertType") String convertType){
        // 将字符串转换成JSON格式
        JSONObject jsonObject = JSONObject.parseObject(textData);
        // 获取其中的属性值
        String input = jsonObject.getString("data");

        String output = converter.convert(input, convertType);

        if (output == null){
            ConvertPlugin convertPlugin = (ConvertPlugin)pluginManager.loadPlugins().get(convertType);
            output = convertPlugin.convert(input);
        }

        return output;
    }


}
